<?php
class AutoAdvController extends CpController{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT Box quảng cáo tự động','link'=>$url->createUrl("autoAdv/index"),'class'=>'');
    }
    public function actionIndex(){
        $row_per_page = 20; 
        $this->breadcrumbs[] = array('name'=>'DS Box quảng cáo tự động','link'=>'#','class'=>'active');
        $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"";
        $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):"";
        $keyword_top = isset($_GET["keyword_top"]) ? trim($_GET["keyword_top"]):"";
        $keyword_bottom = isset($_GET["keyword_bottom"]) ? trim($_GET["keyword_bottom"]):"";
        $group_id = isset($_GET["group_id"]) ? intval($_GET["group_id"]):0;
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        list($max_page,$count,$data) = AAutoAdv::getDataSearch($from_date,$to_date,$keyword_top,$keyword_bottom,$group_id,$status,$page,$row_per_page);

        $this->render("index",array("max_page"=>$max_page,"count"=>$count,"data"=>$data,
        "from_date"=>$from_date,"to_date"=>$to_date,
        "keyword_top"=>$keyword_top,
        "keyword_bottom"=>$keyword_bottom,
        "group_id"=>$group_id,
        "status"=>$status,
        "page"=>$page));
    }
    public function actionCreate(){
        $this->breadcrumbs[] = array('name'=>'Thêm box quảng cáo','link'=>'#','class'=>'active');
        $this->render("create");
    }
    public function actionAjaxSaveBanner(){
        $create_user = Yii::app()->user->name;
        $time_now = time();
        $title_top = isset($_POST["title_top"]) ? trim($_POST["title_top"]):"";
        $title_bottom = isset($_POST["title_bottom"]) ? trim($_POST["title_bottom"]):"";
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]):"";
        $link = isset($_POST["link"]) ? trim($_POST["link"]):"";
        $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]):0;
        $content_id = isset($_POST["content_id"]) ? intval($_POST["content_id"]):0;
        $content_type = isset($_POST["content_type"]) ? intval($_POST["content_type"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $target = isset($_POST["target"]) ? intval($_POST["target"]):0;
        if($picture==""){echo "Chưa upload ảnh";exit;}
        if($group_id <=0 ){echo "Chưa chọn nhóm";exit;}
        $data = array(
        "title_top"=>array("value"=>$title_top,"type"=>2),
        "title_bottom"=>array("value"=>$title_bottom,"type"=>2),
        "picture"=>array("value"=>$picture,"type"=>2),
        "link"=>array("value"=>$link,"type"=>2),
        "status"=>array("value"=>$status,"type"=>1),
        "target"=>array("value"=>$target,"type"=>1),
        "group_id"=>array("value"=>$group_id,"type"=>1),
        "content_id"=>array("value"=>$content_id,"type"=>1),
        "content_type"=>array("value"=>$content_type,"type"=>1),
        "create_date"=>array("value"=>$time_now,"type"=>1),
        "create_user"=>array("value"=>$create_user,"type"=>2),
        );
        $table = "c_auto_adv";
        $last_id = CommonDB::insertRow($table,$data);
        if($last_id >0){
            $act = 1;
            $obj = 21;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$last_id.',title: '.$title_top.',group_id: '.$group_id.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit;
        }else{
            echo "Dữ liệu chưa được thêm";exit;
        }
    }
    public function actionAjaxQuickUpdate(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;   
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        if($id >0){
            $data_old = AAutoAdv::getDataById($id);
            $data = array(
            "status"=>array("value"=>$status,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_auto_adv";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 21;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',status: '.$data_old["status"].'}';
                $content .= '{id: '.$id.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit;
            }else{
                echo "Chưa thay đổi dữ liệu";exit;
            }
        }
    }
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $this->breadcrumbs[] = array('name'=>'Sửa Box quảng cáo tự động','link'=>'#','class'=>'active');
        $data = AAutoAdv::getDataById($id);
        $this->render("edit",array("data"=>$data));
    }
    public function actionAjaxUpdateBanner(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $title_top = isset($_POST["title_top"]) ? trim($_POST["title_top"]):"";
        $title_bottom = isset($_POST["title_bottom"]) ? trim($_POST["title_bottom"]):"";
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]):"";
        $link = isset($_POST["link"]) ? trim($_POST["link"]):"";
        $status = isset($_POST["status"]) ? intval($_POST["status"]):1;
        $target = isset($_POST["target"]) ? intval($_POST["target"]):0;
        $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]):0;
        $content_id = isset($_POST["content_id"]) ? intval($_POST["content_id"]):0;
        $content_type = isset($_POST["content_type"]) ? intval($_POST["content_type"]):0;
        if($picture==""){echo "Chưa upload ảnh";exit;}
        if($group_id <=0 ){echo "Chưa chọn nhóm";exit;}  
        if($id >0){
            $data_old = AAutoAdv::getDataById($id); 
            $data = array(
            "title_top"=>array("value"=>$title_top,"type"=>2),
            "title_bottom"=>array("value"=>$title_bottom,"type"=>2),
            "picture"=>array("value"=>$picture,"type"=>2),
            "link"=>array("value"=>$link,"type"=>2),
            "status"=>array("value"=>$status,"type"=>1),
            "target"=>array("value"=>$target,"type"=>1),
            "group_id"=>array("value"=>$group_id,"type"=>1),
            "content_id"=>array("value"=>$content_id,"type"=>1),
            "content_type"=>array("value"=>$content_type,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_auto_adv";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 21;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',title: '.$data_old["title_top"].',status: '.$data_old["status"].'}';
                $content .= '{id: '.$id.',title: '.$title_top.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit;
            }else{
                echo "Chưa thay đổi dữ liệu";exit;
            }
        }
    }
    public function actionAjaxDelete(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AAutoAdv::getDataById($id);
            $table = "c_auto_adv";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 21;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',title: '.$data_old["title_top"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit;
            }else{
                echo "Chưa thay đổi dữ liệu";exit;
            }
        }
    }

    private function setHtmlJs($group_id){
        $adv = AAutoAdv::getDataByGroup($group_id);
        foreach($adv as $key=>$value){
            $html = $this->renderPartial("application.views.config.tpl_auto_adv",array("data"=>$value),true);
            $data[] = Common::sanitize_output($html);   
        }
        Common::writeFileAdmin($group_id,$data);
    }
}
